package mh.demo.jdk.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 用于分隔字符串例子
 * 阅读Spring源码时发现有在这里使用org.springframework.util.StringUtils.tokenizeToStringArray()
 */
public class StringTokenizerDemo {

    /**
     * 默认分隔符例子
     */
    public List<String> defaultDelimiterDemo() {
        List<String> strList = new ArrayList<>();
        // 默认的分隔符是空格("")、制表符(\t)、换行符(\n)、回车符(\r)。
        StringTokenizer st1 = new StringTokenizer("How are you");
        while (st1.hasMoreTokens()) {
            strList.add(st1.nextToken());
        }
        return strList;
    }

    /**
     * 指定分隔符进行分割字符串
     */
    public List<String> specifiedDelimiterDemo() {
        List<String> strList = new ArrayList<>();
        // 指定分隔符进行分割字符串
        StringTokenizer st1 = new StringTokenizer("How,are;you", ",;");
        while (st1.hasMoreTokens()) {
            strList.add(st1.nextToken());
        }
        return strList;
    }

    /**
     * 指定分隔符进行分割字符串，带有分隔符
     */
    public List<String> returnDlimsDemo() {
        List<String> strList = new ArrayList<>();
        // 指定分隔符进行分割字符串
        StringTokenizer st1 = new StringTokenizer("How;are;you", ";", true);
        while (st1.hasMoreTokens()) {
            strList.add(st1.nextToken());
        }
        return strList;
    }

}
